﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using ALiteTeam.PackageTracer.Data;
using GalaSoft.MvvmLight.Messaging;
using ALiteTeam.PackageTracer.Core;
using ALiteTeam.PackageTracer.Core.Impl;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.Phone.Controls;

namespace ALiteTeam.PackageTracer.ViewModel
{
    public class SelectorViewModel : ViewModelBase
    {
        public IDeliveryOperate DeliveryOp { get; set; }
        public IOrderOperate OrderOp{get;set;}
        public ILogWrite LogWriter { get; set; }

        public SelectorViewModel()
        {
            if (IsInDesignMode)
            {
            }
            else
            {
                Initialize();

                this.NavigationToInputCommand = new RelayCommand<LongListSelector>(NavigationToInput);
            }
        }

        private void Initialize()
        {
            this.DeliveryOp = DeliveryDataOp.Instance;
            this.LogWriter = new GeneralLogWriter();

            this.DeliveryGroups = new ObservableCollection<PublicGrouping<string, Delivery>>(this.DeliveryOp.GetDeliveryGroups());
        }

        /// <summary>
        /// The <see cref="DeliveryGroups" /> property's name.
        /// </summary>
        public const string DeliveryGroupsPropertyName = "DeliveryGroups";
        private ObservableCollection<PublicGrouping<string, Delivery>> mDeliveryGroups = null;

        /// <summary>
        /// Gets the DeliveryGroups property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ObservableCollection<PublicGrouping<string, Delivery>> DeliveryGroups
        {
            get
            {
                return mDeliveryGroups;
            }

            set
            {
                if (mDeliveryGroups == value)
                {
                    return;
                }
                mDeliveryGroups = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(DeliveryGroupsPropertyName);
            }
        }

        public RelayCommand<LongListSelector> NavigationToInputCommand { get; private set; }
        private void NavigationToInput(LongListSelector longList)
        {
            if (longList.SelectedItem != null)
            {
                var delivery = longList.SelectedItem as Delivery;

                Messenger.Default.Send<Guid>(delivery.ID, InputOrderViewModel.INITIALIZE);
                (Application.Current.RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).Navigate(new Uri("/View/InputOrder.xaml", UriKind.Relative));
            }
        }
    }
}
